package com.lotus.smstest;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SMSTest extends Activity {
	private Button sendButton;
	private EditText etNum, etContent;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        sendButton = (Button) findViewById(R.id.btOk);
        etNum = (EditText) findViewById(R.id.etnum);
        etContent = (EditText) findViewById(R.id.etcontent);
        
        sendButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				String num = etNum.getText().toString();
				String content = etContent.getText().toString();
				if (num.length() > 0 && content.length() > 0)
					sendSMS(num, content);
				else {
					Toast.makeText(SMSTest.this, "num and content must not be empty!", Toast.LENGTH_LONG).show();
				}
			}

		});
    }
    
/*    private void sendSMS(String num, String content) {
    	PendingIntent pd = PendingIntent.getActivity(SMSTest.this, 0, new Intent(this, SMSTest.class), 0);
    	SmsManager smsManager = SmsManager.getDefault();
    	smsManager.sendTextMessage(num, null, content, pd, null);
    }*/
    
    private void sendSMS(String num, String content) {
    	String SENT = "SMS_SENT" ; 
    	String DELIVERED = "SMS_DELIVERED" ; 
    	PendingIntent send = PendingIntent.getBroadcast(SMSTest.this, 0, new Intent(SENT), 0);
    	PendingIntent delIntent = PendingIntent.getBroadcast(SMSTest.this, 0, new Intent(DELIVERED), 0);
		
    	registerReceiver(new BroadcastReceiver() {
			
			@Override
			public void onReceive(Context context, Intent intent) {
				switch (this.getResultCode()) {
				case Activity.RESULT_OK :
                    Toast. makeText ( getBaseContext() , "SMS sent" , 
                            Toast. LENGTH_SHORT ) .show (); 
                    break ; 
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE :
                    Toast. makeText ( getBaseContext() , "Generic failure" , 
                            Toast. LENGTH_SHORT ) .show (); 
                    break ; 
                case SmsManager.RESULT_ERROR_NO_SERVICE :
                    Toast. makeText ( getBaseContext() , "No service" , 
                            Toast. LENGTH_SHORT ) .show (); 
                    break ; 
                case SmsManager.RESULT_ERROR_NULL_PDU :
                    Toast. makeText ( getBaseContext() , "Null PDU" , 
                            Toast. LENGTH_SHORT ) .show (); 
                    break ; 
                case SmsManager.RESULT_ERROR_RADIO_OFF :
                    Toast. makeText ( getBaseContext() , "Radio off" , 
                            Toast. LENGTH_SHORT ) .show (); 
                    break ; 
				default:
					break;
				}
			}
		}, new IntentFilter(SENT));
    	
    	registerReceiver(new BroadcastReceiver() {
			
			@Override
			public void onReceive(Context context, Intent intent) {
				switch ( getResultCode()) 
                { 
                    case Activity.RESULT_OK :
                        Toast. makeText ( getBaseContext() , "SMS delivered" , 
                                Toast. LENGTH_SHORT ) .show (); 
                        break ; 
                    case Activity.RESULT_CANCELED :
                        Toast. makeText ( getBaseContext() , "SMS not delivered" , 
                                Toast. LENGTH_SHORT ) .show (); 
                        break ;                         
                } 
			}
		}, new IntentFilter(DELIVERED));
    	
    	SmsManager sms = SmsManager. getDefault (); 
    	sms. sendTextMessage (num, null , content, send, delIntent);    
    }
}